package se.perp.c4.control.bot;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import se.perp.c4.model.Actor;
import se.perp.c4.model.Actor.EActor;
import se.perp.c4.model.Move;
import se.perp.c4.model.Territory;
import se.perp.c4.model.Territory.ETerritory;
import se.perp.c4.model.TerritoryCollection;
import se.perp.c4.net.Message;
import static se.perp.c4.model.Actor.Side.*;
import static se.perp.c4.model.Territory.ETerritory.*;

class IdiotBot extends Bot {

    public IdiotBot(IBotSideConnection conn) {
        super(conn);
    }

    protected Move createMove() {
        Actor attacker = null;
        Actor defender = null;
        Territory to = null;
        boolean moveOK = false;
        boolean hostile=false;
        while (!moveOK) {
            attacker = pickAttacker();
            to = pickTargetTerritory(attacker);

            boolean friendlyAndFree = to.isOccupiedBy(botSide) || to.getOccupantCount() == 0;
            hostile  = !to.isOccupiedBy(botSide) && to.getOccupantCount() > 0;
            moveOK = friendlyAndFree || hostile;
            System.out.println("Can "+attacker+" move to "+to+"? " + (moveOK? "Yes!":"No!"));
        }
        if (hostile) {
            // Pick a target
            int i = (int) (Math.random() * to.getOccupantCount());
            defender = to.getOccupants().get(i);
        }
        EActor atk;
        EActor def = null;
        ETerritory eFrom;
        ETerritory eTo;
        atk = attacker.who;
        if (defender != null) {
            def = defender.who;
        }
        eFrom = attacker.where;
        eTo = to.which;
        Move move = new Move(atk, def, eFrom, eTo);
        System.out.println("Bot decides move: "+move);
        return move;
    }

    protected Map<EActor, ETerritory> createPlacement() {
        Map<EActor, ETerritory> placement = new HashMap<EActor, ETerritory>();
        EActor[] botActors = botSide == LIGHT ? Actor.LIGHT_ACTORS : Actor.DARK_ACTORS;
        ArrayList<Actor> actors = new ArrayList<Actor>();
        for (EActor ea : botActors) {
            Actor a = actorColl.get(ea);
            actors.add(a);
        }
        Collections.shuffle(actors);
        for (ETerritory et : TerritoryCollection.placementTerritories(botSide)) {
            if (et == MORDOR || et == SHIRE) {
                for (int i = 0; i < 4; i++) {
                    Actor a = actors.remove(0);
                    placement.put(a.who, et);
                    terrColl.get(et).addActor(a);
                }
            } else {
                Actor a = actors.remove(0);
                placement.put(a.who, et);
                terrColl.get(et).addActor(a);
            }
        }
        assert actors.isEmpty();
        return placement;
    }

    private Actor pickAttacker() {
        ArrayList<Actor> living = new ArrayList<Actor>();
        for (Actor a : actorColl) {
            if (a.isAlive && a.side == botSide) {
                living.add(a);
            }
        }
        int i = (int) (Math.random() * living.size());
        return living.get(i);
    }

    private Territory pickTargetTerritory(Actor attacker) {
        assert attacker.where != null;

        Territory t = terrColl.get(attacker.where);
        ETerritory[] candidates = (botSide == DARK ? t.darkNeighbors : t.lightNeighbors);
        int i = (int) (Math.random() * candidates.length);
        return terrColl.get(candidates[i]);
    }

    @Override
    protected void handleMsg_battleMove(Message m) {
    }
}
